package se3a04.mcmapster;

import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class Maps extends MapActivity {

	private MapView mapView;
	private MyLocationOverlay myLocationOverlay;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // main.xml contains a MapView
        setContentView(R.layout.mapview); 
        
        // extract MapView from layout
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		// create an overlay that shows our current location
		myLocationOverlay = new myLocation(this, mapView);
		
		// add this overlay to the MapView and refresh it
		mapView.getOverlays().add(myLocationOverlay);
		mapView.postInvalidate();
		
		//Go to McMaster
		GeoPoint mcmaster = new GeoPoint(43262592, -79919251);
        mapView.getController().animateTo(mcmaster);
        mapView.getController().setZoom(17);
		
		// call convenience method that zooms map on our location
		zoomToMyLocation();
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		// when our activity resumes, we want to register for location updates
		myLocationOverlay.enableMyLocation();
	}

	@Override
	protected void onPause() {
		super.onPause();
		// when our activity pauses, we want to remove listening for location updates
		myLocationOverlay.disableMyLocation();
	}
	
	/**
	 * This method zooms to the user's location with a zoom level of 10.
	 */
	private void zoomToMyLocation() {
		GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
		if(myLocationGeoPoint != null) {
			mapView.getController().animateTo(myLocationGeoPoint);
			mapView.getController().setZoom(19);
		}
		else {
			Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
		}
	}
	
	//@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
	
	//@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//Handle main menu item selection
		switch (item.getItemId()) {
		case R.id.new_bookmark:
			newBookmark();
			return true;
		case R.id.change_mode:
			changeMode();
			return true;
		case R.id.change_view:
			changeView();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	public boolean changeView() {
		if (mapView.isSatellite()){
			mapView.setSatellite(false);
			mapView.setStreetView(true);
		}else{
			mapView.setStreetView(false);
			mapView.setSatellite(true);
		}
		return true;
	}
	
	public boolean changeMode() {
		return true;
	}
	
	public boolean newBookmark() {
		/*OverlayItem overlayBookmark = new OverlayItem(touchedPoint, "Hello", "Second");
		bookmark custom = new bookmark(d, Maps.this);
		custom.insertBookmark(overlayBookmark);
		overlayList.add(custom);*/
		//Touchy t = new Touchy;
		
		return true;
	}
	
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}